package cn.iocoder.yudao.module.hs.service.rawmaterialorder;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.hs.controller.admin.rawmaterialorder.vo.RawMaterialOrderPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.rawmaterialorder.vo.RawMaterialOrderSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.rawmaterialorder.RawMaterialOrderDO;
import cn.iocoder.yudao.module.hs.dal.mysql.rawmaterialorder.RawMaterialOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.RAW_MATERIAL_ORDER_NOT_EXISTS;

/**
 * 领料单信息 Service 实现类
 *
 * @author 航盛
 */
@Service
@Validated
public class RawMaterialOrderServiceImpl implements RawMaterialOrderService {

    @Resource
    private RawMaterialOrderMapper rawMaterialOrderMapper;

    @Override
    public Long createRawMaterialOrder(RawMaterialOrderSaveReqVO createReqVO) {
        // 插入
        RawMaterialOrderDO rawMaterialOrder = BeanUtils.toBean(createReqVO, RawMaterialOrderDO.class);
        rawMaterialOrderMapper.insert(rawMaterialOrder);
        // 返回
        return rawMaterialOrder.getId();
    }

    @Override
    public void updateRawMaterialOrder(RawMaterialOrderSaveReqVO updateReqVO) {
        // 校验存在
        validateRawMaterialOrderExists(updateReqVO.getId());
        // 更新
        RawMaterialOrderDO updateObj = BeanUtils.toBean(updateReqVO, RawMaterialOrderDO.class);
        rawMaterialOrderMapper.updateById(updateObj);
    }

    @Override
    public void deleteRawMaterialOrder(Long id) {
        // 校验存在
        validateRawMaterialOrderExists(id);
        // 删除
        rawMaterialOrderMapper.deleteById(id);
    }

    private void validateRawMaterialOrderExists(Long id) {
        if (rawMaterialOrderMapper.selectById(id) == null) {
            throw exception(RAW_MATERIAL_ORDER_NOT_EXISTS);
        }
    }

    @Override
    public RawMaterialOrderDO getRawMaterialOrder(Long id) {
        return rawMaterialOrderMapper.selectById(id);
    }

    @Override
    public PageResult<RawMaterialOrderDO> getRawMaterialOrderPage(RawMaterialOrderPageReqVO pageReqVO) {
        return rawMaterialOrderMapper.selectPage(pageReqVO);
    }

}